﻿using DotNetExtensions.Core;
using DotNetExtensions.Core.Encryptions;
using DotNetExtensions.Core.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static DotNetExtensions.Core.Http.HttpClientFactory;

namespace CoreSimple
{
    class RestfulHttpClientSimple
    {
        //    IRestfulHttpReqeust restfulHttpClient;
        //    public RestfulHttpClientSimple()
        //    {
        //        restfulHttpClient = (IRestfulHttpReqeust)HttpClientFactory.Default.Create(HttpClientKind.Restful);

        //    }
        //    public void TestGet()
        //    {
        //        var resp = restfulHttpClient.Get("http://192.168.10.27:8100/manage/personnel/queryPersonnel.do?t=1654732706361&userName=RMTEST55&state=1&page=1&pageSize=25&moduleType=0", null, new Cookie[] { new Cookie("JSESSIONID", "E784140864F177B67EB23FC012AEA73F") });
        //        Console.WriteLine(resp.ToString(Encoding.UTF8));
        //    }
        //    public void TestCookie()
        //    {
        //        //var resp = restfulHttpClient.Get("http://www.baidu.com", null);
        //        //string[] cookies = resp.Cookies.Select(c => $"{c.Name}={c.Value}").ToArray();
        //        //foreach (var item in cookies)
        //        //{
        //        //    Console.WriteLine(item);
        //        //}

        //        string json = "{ \"AcctId\":\"632e83b9e2e639\",\"UserName\": \"demo\",\"Password\":\"serviceup.CN2022\",\"lcid\":2052 }";

        //        var resp = restfulHttpClient.Post("http://localhost/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc", Encoding.UTF8.GetBytes(json));
        //        if (resp.OK) Console.WriteLine("登录成功");
        //        string[] cookies = resp.Cookies.Select(c => $"{c.Name}={c.Value}").ToArray();
        //        foreach (var item in cookies)
        //        {
        //            Console.WriteLine(item);
        //        }
        //        json = "{ \"paramters\":{  \"Code\":\"1.01.001.0002\"} }";
        //        resp = restfulHttpClient.Post("http://localhost/K3Cloud/Demo.KD.Service.Material.GetMaterialList.ExcuteService,Demo.KD.Service.common.kdsvc", Encoding.UTF8.GetBytes(json));

        //        Console.WriteLine(resp.ToString(Encoding.UTF8));
        //    }

        //    public class GetRawData
        //    {
        //        public string Password { get; set; }
        //        public string TenantLoginCode { get; set; }
        //        public string Username { get; set; }
        //    }
        //    public class GetTokenData
        //    {
        //        public string access_token { get; set; }
        //        public List<string> permissions { get; set; }
        //        public int expires_in { get; set; }
        //        public object user { get; set; }
        //    }
        //    public void TestToken()
        //    {
        //        //string token = TestGetRaw();
        //        //string data = "{\"beginTime\":\"2022-08-11\",\"endTime\":\"2022-08-13\",\"missionMember\":[{\"disabled\":1,\"remark\":\"\",\"taskMemberId\":1002208050000271,\"taskRole\":1}],\"projectCode\":\"P202208120001\",\"taskCode\":\"RW02\",\"taskDescription\":\"开工动员大会\",\"taskName\":\"开工动员大会\"}";
        //        //string url = "http://124.71.83.128:11000/adapter/projectTasks/addTaskMaster";

        //        //byte[] content = Encoding.UTF8.GetBytes(data);

        //        //restfulHttpClient.Token = token;
        //        //var resp = restfulHttpClient.Post(url, content, new Header[] { new Header("Content-Type", "application/json") }, null);

        //        //Console.WriteLine(resp.ToString(Encoding.UTF8));
        //    }

        //    public void TestGetFormData()
        //    {
        //        //string token = TestGetRaw();
        //        //string url = "http://124.71.83.128:11000/adapter/projectTasks/deleteTaskMaster";
        //        //string json = JsonConvert.SerializeObject(new List<KeyValue>() { new KeyValue("taskCode", "wbsMO-0005") });
        //        //byte[] content = Encoding.UTF8.GetBytes(json);
        //        //restfulHttpClient.Token = token;
        //        //var resp = restfulHttpClient.Get(url, content, new Header[] { ContentType.FormData }, null);

        //        //Console.WriteLine(resp.ToString());
        //    }
        //    public string TestGetRaw()
        //    {

        //        //var data = new GetRawData()
        //        //{
        //        //    Password = "admin123",
        //        //    Username = "admin",
        //        //    TenantLoginCode = "admin"
        //        //};

        //        //string json = JsonConvert.SerializeObject(data, new JsonSerializerSettings()
        //        //{
        //        //    ContractResolver = new CamelCasePropertyNamesContractResolver()
        //        //});

        //        //byte[] content = Encoding.UTF8.GetBytes(json);


        //        //var resp = restfulHttpClient.Post("http://124.71.83.128:11000/adapter/basis/getToken", content, new Header[] { new Header("Content-Type", "application/json") }, null);

        //        //Console.WriteLine(resp.ToString(Encoding.UTF8));

        //        //return resp.To<GetTokenData>().Data.access_token;
        //    }

        //    public void TestMultipleFormData()
        //    {
        //        //MultipartFormDataItem[] data = new MultipartFormDataItem[1];
        //        //data[0] = new MultipartFormDataItem
        //        //{
        //        //    Name = "file",
        //        //    Value = @"C:\Users\Administrator\Desktop\补录照片\test.jpg",
        //        //    Type = MultipartFormDataType.Stream
        //        //};

        //        //var resp = restfulHttpClient.Send("http://192.168.10.27:8100/manage/personnel/faceDetectionAndCompression.do", data, null, new Cookie[] { new Cookie("JSESSIONID", "E784140864F177B67EB23FC012AEA73F") });

        //        //Console.WriteLine(resp.ToString(Encoding.UTF8));
        //    }

        //    public void TestMultipleFormData2()
        //    {
        //        MultipartFormDataItem[] data = new MultipartFormDataItem[1];
        //        data[0] = new MultipartFormDataItem
        //        {
        //            Name = "file",
        //            Value = System.IO.File.ReadAllBytes(@"C:\Users\Administrator\Desktop\补录照片\test.jpg"),
        //            Type = MultipartFormDataType.Stream
        //        };

        //        var resp = restfulHttpClient.Send("http://192.168.10.27:8100/manage/personnel/faceDetectionAndCompression.do", data, null, new Cookie[] { new Cookie("JSESSIONID", "E784140864F177B67EB23FC012AEA73F") });

        //        Console.WriteLine(resp.ToString(Encoding.UTF8));
        //    }

        //    public void TestFormData()
        //    {
        //        List<KeyValue> keyValues = new List<KeyValue>();
        //        keyValues.Add(new KeyValue("userName", "admin"));
        //        keyValues.Add(new KeyValue("pwd", MD5.Hash("123654").ToLower()));
        //        keyValues.Add(new KeyValue("platformType", "3"));
        //        keyValues.Add(new KeyValue("clientVersion", "0.1"));
        //        keyValues.Add(new KeyValue("needErrorTip", "true"));
        //        keyValues.Add(new KeyValue("clientTime", DateTime.Now.ToString("yyyy-MM-dd")));

        //        string body = JsonConvert.SerializeObject(keyValues);

        //        var resp = restfulHttpClient.Post("http://192.168.10.27:8100/manage/login/loginOn.do?t=1654681538768", Encoding.UTF8.GetBytes(body), new Header[] { new Header("Content-Type", "application/x-www-form-urlencoded") });
        //        Console.WriteLine(resp.ToString(Encoding.UTF8));
        //    }

        //    public void TestJsonData()
        //    {
        //        string data = "{\"userList\": [{\"id\": 2065,\"name\": \"测试555\",\"employId\": \"RMTEST55\"}]}";

        //        var resp = restfulHttpClient.Post("http://192.168.10.27:8100/manage/personnel/deletePersonnel.do?t=1654732791866", Encoding.UTF8.GetBytes(data), null, new Cookie[] { new Cookie("JSESSIONID", "E784140864F177B67EB23FC012AEA73F") });

        //        Console.WriteLine(resp.ToString(Encoding.UTF8));
        //    }

        //    public void TestJsonData2()
        //    {
        //        string data = "{\"userTpm\":{\"userName\":\"RMTEST55\",\"branchId\":\"1\",\"employId\":\"RMTEST55\",\"name\":\"测试555\",\"password\":\"9422c1cfc92bf28fe7ac1663a1d99ab6\",\"mobiAttPerm\":0,\"postId\":1,\"gender\":0,\"entryDate\":\"2022-06-08 00:00:00\",\"startWorkDate\":\"2022-06-08 00:00:00\",\"effectiveDate\":\"2050-01-01 00:00:00\",\"leader\":0,\"fileList\":[\"users/2022-06-08/0bf2ce26.jpg\"],\"graduateTime\":\"Invalid date 00:00:00\",\"moduleType\":0}}";

        //        var resp = restfulHttpClient.Post("http://192.168.10.27:8100/manage/personnel/insertPersonnel.do", Encoding.UTF8.GetBytes(data), null, new Cookie[] { new Cookie("JSESSIONID", "E784140864F177B67EB23FC012AEA73F") });

        //        Console.WriteLine(resp.ToString(Encoding.UTF8));
        //    }
    }
}
